package com.soccertgc.core.game.requests
{
	import com.soccertgc.core.DataObject;
	import com.soccertgc.core.game.GameServerStatus;
	
	/**
	 * 
	 * @author ltavecchia 
	 */	
	public class GameRequestResult extends DataObject
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------
		
		// JSON properties names
		protected static const		JSON_PROPERTY_RESULT:String             = "result";
		protected static const		JSON_PROPERTY_ERROR:String            	= "error";
		protected static const		JSON_PROPERTY_DATA:String            	= "data";
		
		// Default values
		public static const         DEFAULT_DATA:Object						= null;
		public static const         DEFAULT_ERROR:Object					= null;
		public static const         DEFAULT_MESSAGE:String					= "";
		public static const         DEFAULT_CODE:GameRequestResultCode		= GameRequestResultCode.UNKNOWN;
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _code:GameRequestResultCode;
		private var _data:Object;
		private var _error:GameRequestError;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTOR
		// -------------------------------------------------------------------------------		
		
		public function GameRequestResult(data:Object = null)
		{
			// Clear data
			clear();
			// If data is provided, initializes data object
			if (data != null) load(data);
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------
		
		public function get code():GameRequestResultCode
		{
			return _code;
		}
		
		public function get waiting():Boolean
		{
			return _code == GameRequestResultCode.WAIT;
		}
		
		public function get succeeded():Boolean
		{
			return _code == GameRequestResultCode.SUCCESS;
		}
		
		public function get isInGameEnd():Boolean
		{
			return	_code == GameRequestResultCode.GAME_END;
		}
		
		public function get isInError():Boolean
		{
			return	_code == GameRequestResultCode.ERROR ||
					_code == GameRequestResultCode.EXCEPTION;
		}

		public function get data():Object
		{
			return _data;
		}
		
		public function get hasData():Boolean
		{
			return _data != null;
		}

		public function get error():GameRequestError
		{
			return _error;
		}
		
		public function get hasError():Boolean
		{
			return _error != null;
		}
		
		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		// DataObject implementation
		
		override protected function validate(data:Object):void
		{
			if (!data.hasOwnProperty(JSON_PROPERTY_RESULT))		throwLoadMissingPropError(JSON_PROPERTY_RESULT);
			if (!data.hasOwnProperty(JSON_PROPERTY_ERROR))		throwLoadMissingPropError(JSON_PROPERTY_ERROR);
			if (!data.hasOwnProperty(JSON_PROPERTY_DATA))		throwLoadMissingPropError(JSON_PROPERTY_DATA);
		}
		
		override protected function fill(data:Object):void
		{
			// Retrieve game request result code value
			var resultCode:int = int(data[JSON_PROPERTY_RESULT]);
			// Retrieve game request result code
			_code = GameRequestResultCode.getById(resultCode);
			// Parsing by the result code
			switch(_code)
			{
				case GameRequestResultCode.WAIT:
				case GameRequestResultCode.SUCCESS:
				case GameRequestResultCode.GAME_END:
				{
					// Retriev raw data
					_data = data[JSON_PROPERTY_DATA];
					// Resets the error
					_error = null;
					break;
				}
					
				case GameRequestResultCode.ERROR:
				case GameRequestResultCode.EXCEPTION:
				{
					_data	= data[JSON_PROPERTY_DATA];
					_error	= new GameRequestError(data[JSON_PROPERTY_ERROR]);
					break;
				}
			}
		}

		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		override public function clear():void
		{
			_code			= GameRequestResultCode.UNKNOWN;
			_data			= null;
			_error			= null;
		}	
		
	}
}